package com.yilin.medical.views.wheelview;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.yilin.medical.R;
import com.yilin.medical.views.wheelview.WheelView.OnWheelChangedListener;

public class CalcWheelDialog extends Dialog {
	private List<String> areaList;
	private TextView mtextTextViewTitle;
	private TextView mTextViewOk;
	private WheelView mWheelView;
	int currentPosition;

	private android.view.View.OnClickListener ListenerOk;
	


	public android.view.View.OnClickListener getListenerOk() {
		return ListenerOk;
	}

	public void setListenerOk(android.view.View.OnClickListener listenerOk) {
		ListenerOk = listenerOk;
	}


	private Context context;
	OnWheelChangedListener wheelChangedListener;

	public CalcWheelDialog(Context context, List<String> areaList,
			OnWheelChangedListener wheelChangedListener, int currentPosition) {
		super(context, R.style.Dialog_Fullscreen);
		this.context = context;
		this.areaList = areaList;
		Window window = this.getWindow();
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.mystyle);
		this.wheelChangedListener = wheelChangedListener;
		this.currentPosition = currentPosition;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.jx_wheel_calc_dialog);
		mtextTextViewTitle = (TextView) findViewById(R.id.tv_calc_title);
		mTextViewOk = (TextView) findViewById(R.id.tv_ok);
		mTextViewOk.setOnClickListener(ListenerOk);
		mWheelView = (WheelView) findViewById(R.id.wv_calc);
		this.getWindow().setLayout(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		initiWheelView();
	}

	private void initiWheelView() {
		mWheelView.setAdapter(new ListWheelAdapter(areaList));
		mWheelView.setVisibleItems(7);
		mWheelView.setCyclic(false);
		mWheelView.setCurrentItem(currentPosition);
		mWheelView.addChangingListener(wheelChangedListener);
	}

	
	public TextView getMtextTextViewTitle() {
		return mtextTextViewTitle;
	}

	public void setMtextTextViewTitle(TextView mtextTextViewTitle) {
		this.mtextTextViewTitle = mtextTextViewTitle;
	}

	public TextView getmTextViewOk() {
		return mTextViewOk;
	}

	public void setmTextViewOk(TextView mTextViewOk) {
		this.mTextViewOk = mTextViewOk;
	}
	
	
	private class ListWheelAdapter implements WheelAdapter {

		/** The default items length */
		public static final int DEFAULT_LENGTH = -1;

		// items
		private List<String> items;

		// length
		private int length;

		/**
		 * Constructor
		 * 
		 * @param items
		 *            the items
		 * @param length
		 *            the max items length
		 */
		public ListWheelAdapter(List<String> items, int length) {
			this.items = items;
			this.length = length;
		}

		/**
		 * Contructor
		 * 
		 * @param items
		 *            the items
		 */
		public ListWheelAdapter(List<String> items) {
			this(items, DEFAULT_LENGTH);
		}

		public int getItemsCount() {

			return items == null ? 0 : items.size();
		}

		public int getMaximumLength() {
			return length;
		}

		@Override
		public String getItem(int index) {
			// TODO Auto-generated method stub
			return items.get(index);
		}
	}

}
